package com.faithnmn.learnenglishbc.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.faithnmn.learnenglishbc.MainActivity;
import com.faithnmn.learnenglishbc.R;
import com.faithnmn.learnenglishbc.adapter.StableArrayAdapter;
import com.faithnmn.learnenglishbc.listener.ChangeFragmentListener;
import com.faithnmn.learnenglishbc.util.LessonAudio;
import com.faithnmn.learnenglishbc.util.LessonLyrics;
import com.faithnmn.learnenglishbc.util.LessonUtil;
import com.faithnmn.utils.core.model.Configurations;

public class SeriFragment extends Fragment {

	private ChangeFragmentListener changeFragmentListener;
	private int indexSeri;
	private String title;
	private StableArrayAdapter adapter;
	private ArrayList<Configurations> configurations;
	public SeriFragment(){};
	
	public void setChangeFragmentListener(ChangeFragmentListener changeFragmentListener){
		this.changeFragmentListener  = changeFragmentListener;
	}
	
	public static SeriFragment getInstance(int indexSeri,String title){
		SeriFragment fragment = new SeriFragment();
		Bundle bundle = new Bundle();
		bundle.putString("title", title);
		bundle.putInt("index", indexSeri);
		fragment.setArguments(bundle);
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View seriLayout = inflater.inflate(R.layout.fragment_seri_list, null);
		indexSeri = getArguments().getInt("index");
		title = getArguments().getString("title");
		((MainActivity)getActivity()).getChangeActionbarTitle().changeActionBarTitle(title);
		ListView listSeris = (ListView) seriLayout.findViewById(R.id.seri_list);
		adapter = new StableArrayAdapter(getActivity(),inflater, android.R.layout.simple_list_item_1, LessonUtil.getLessionList(indexSeri),null);
		adapter.setIndexSeries(indexSeri);
		adapter.setShowPlayingIcon(true, indexSeri);
		adapter.setShowSavedIndicator(true);
		listSeris.setAdapter(adapter);
		listSeris.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
				openLession(position);
			}
		});
		return seriLayout;
	}
	
	@Override
	public void onDestroyView() {
		((MainActivity)getActivity()).getChangeActionbarTitle().changeActionBarTitle("Learn English BC");
		super.onDestroyView();
	}
	
	private void openLession(int position) {
		LessionFragment lession = null;
		title = adapter.getLessionInfo(position).udTitle;
		if(indexSeri == 0){
			switch (position) {
			case 0:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep1,LessonAudio.ep1,indexSeri,position);
				break;
			case 1:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep2,LessonAudio.ep2,indexSeri,position);
				break;
			case 2:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep3,LessonAudio.ep3,indexSeri,position);
				break;
			case 3:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep4,LessonAudio.ep4,indexSeri,position);
				break;
			case 4:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep5,LessonAudio.ep5,indexSeri,position);
				break;
			case 5:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep6,LessonAudio.ep6,indexSeri,position);
				break;
			case 6:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep7,LessonAudio.ep7,indexSeri,position);
				break;
			case 7:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep8,LessonAudio.ep8,indexSeri,position);
				break;
			case 8:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep9,LessonAudio.ep9,indexSeri,position);
				break;
			case 9:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep10,LessonAudio.ep10,indexSeri,position);
				break;
			case 10:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep11,LessonAudio.ep11,indexSeri,position);
				break;
			case 11:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep12,LessonAudio.ep12,indexSeri,position);
				break;
			default:
				break;
			}
		}else if(indexSeri == 1){
			switch (position) {
			case 0:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep1_s2,LessonAudio.ep1_s2,indexSeri,position);
				break;
			case 1:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep2_s2,LessonAudio.ep2_s2,indexSeri,position);
				break;
			case 2:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep3_s2,LessonAudio.ep3_s2,indexSeri,position);
				break;
			case 3:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep4_s2,LessonAudio.ep4_s2,indexSeri,position);
				break;
			case 4:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep5_s2,LessonAudio.ep5_s2,indexSeri,position);
				break;
			case 5:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep6_s2,LessonAudio.ep6_s2,indexSeri,position);
				break;
			case 6:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep7_s2,LessonAudio.ep7_s2,indexSeri,position);
				break;
			case 7:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep8_s2,LessonAudio.ep8_s2,indexSeri,position);
				break;
			case 8:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep9_s2,LessonAudio.ep9_s2,indexSeri,position);
				break;
			case 9:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep10_s2,LessonAudio.ep10_s2,indexSeri,position);
				break;

			case 10:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep11_s2,LessonAudio.ep11_s2,indexSeri,position);
				break;
			case 11:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep12_s2,LessonAudio.ep12_s2,indexSeri,position);
				break;
			default:
				break;
			}
		}else if(indexSeri == 2){
			switch (position) {
			case 0:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep1_s3,LessonAudio.ep1_s3,indexSeri,position);
				break;
			case 1:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep2_s3,LessonAudio.ep2_s3,indexSeri,position);
				break;
			case 2:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep3_s3,LessonAudio.ep3_s3,indexSeri,position);
				break;
			case 3:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep4_s3,LessonAudio.ep4_s3,indexSeri,position);
				break;
			case 4:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep5_s3,LessonAudio.ep5_s3,indexSeri,position);
				break;
			case 5:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep6_s3,LessonAudio.ep6_s3,indexSeri,position);
				break;
			case 6:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep7_s3,LessonAudio.ep7_s3,indexSeri,position);
				break;
			case 7:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep8_s3,LessonAudio.ep8_s3,indexSeri,position);
				break;
			case 8:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep9_s3,LessonAudio.ep9_s3,indexSeri,position);
				break;
			case 9:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep10_s3,LessonAudio.ep10_s3,indexSeri,position);
				break;

			case 10:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep11_s3,LessonAudio.ep11_s3,indexSeri,position);
				break;
			case 11:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep12_s3,LessonAudio.ep12_s3,indexSeri,position);
				break;
			default:
				break;
			}
		}else{
			switch (position) {
			case 0:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep1_s4,LessonAudio.ep1_s4,indexSeri,position);
				break;
			case 1:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep2_s4,LessonAudio.ep2_s4,indexSeri,position);
				break;
			case 2:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep3_s4,LessonAudio.ep3_s4,indexSeri,position);
				break;
			case 3:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep4_s4,LessonAudio.ep4_s4,indexSeri,position);
				break;
			case 4:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep5_s4,LessonAudio.ep5_s4,indexSeri,position);
				break;
			case 5:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep6_s4,LessonAudio.ep6_s4,indexSeri,position);
				break;
			case 6:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep7_s4,LessonAudio.ep7_s4,indexSeri,position);
				break;
			case 7:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep8_s4,LessonAudio.ep8_s4,indexSeri,position);
				break;
			case 8:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep9_s4,LessonAudio.ep9_s4,indexSeri,position);
				break;
			case 9:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep10_s4,LessonAudio.ep10_s4,indexSeri,position);
				break;

			case 10:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep11_s4,LessonAudio.ep11_s4,indexSeri,position);
				break;
			case 11:
				lession = LessionFragment.getInstance(title, LessonLyrics.ep12_s4,LessonAudio.ep12_s4,indexSeri,position);
				break;
			default:
				break;
			}
		}
		
		if(lession != null && changeFragmentListener != null){
			changeFragmentListener.onDisplayFragment(lession, title);
		}else{
			Toast.makeText(getActivity(), "B\u00E0i h\u1ECDc s\u1EBD \u0111\u01B0\u1EE3c c\u1EADp nh\u1EADt sau..", Toast.LENGTH_SHORT).show();
		}
	}

}
